<?php

namespace App\Http\Controllers\API;

use App\Http\Requests\API\CreateGoodsAttributeAPIRequest;
use App\Http\Requests\API\UpdateGoodsAttributeAPIRequest;
use App\Models\GoodsAttribute;
use App\Repositories\GoodsAttributeRepository;
use Illuminate\Http\Request;
use App\Http\Controllers\AppBaseController;
use InfyOm\Generator\Criteria\LimitOffsetCriteria;
use Prettus\Repository\Criteria\RequestCriteria;
use Response;

/**
 * Class GoodsAttributeController
 * @package App\Http\Controllers\API
 */

class GoodsAttributeAPIController extends AppBaseController
{
    /** @var  GoodsAttributeRepository */
    private $goodsAttributeRepository;

    public function __construct(GoodsAttributeRepository $goodsAttributeRepo)
    {
        $this->goodsAttributeRepository = $goodsAttributeRepo;
    }

    /**
     * Display a listing of the GoodsAttribute.
     * GET|HEAD /goodsAttributes
     *
     * @param Request $request
     * @return Response
     */
    public function index(Request $request)
    {
        $this->goodsAttributeRepository->pushCriteria(new RequestCriteria($request));
        $this->goodsAttributeRepository->pushCriteria(new LimitOffsetCriteria($request));
        $goodsAttributes = $this->goodsAttributeRepository->all();

        return $this->sendResponse($goodsAttributes->toArray(), 'Goods Attributes retrieved successfully');
    }

    /**
     * Store a newly created GoodsAttribute in storage.
     * POST /goodsAttributes
     *
     * @param CreateGoodsAttributeAPIRequest $request
     *
     * @return Response
     */
    public function store(CreateGoodsAttributeAPIRequest $request)
    {
        $input = $request->all();

        $goodsAttributes = $this->goodsAttributeRepository->create($input);

        return $this->sendResponse($goodsAttributes->toArray(), 'Goods Attribute saved successfully');
    }

    /**
     * Display the specified GoodsAttribute.
     * GET|HEAD /goodsAttributes/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function show($id)
    {
        /** @var GoodsAttribute $goodsAttribute */
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            return $this->sendError('Goods Attribute not found');
        }

        return $this->sendResponse($goodsAttribute->toArray(), 'Goods Attribute retrieved successfully');
    }

    /**
     * Update the specified GoodsAttribute in storage.
     * PUT/PATCH /goodsAttributes/{id}
     *
     * @param  int $id
     * @param UpdateGoodsAttributeAPIRequest $request
     *
     * @return Response
     */
    public function update($id, UpdateGoodsAttributeAPIRequest $request)
    {
        $input = $request->all();

        /** @var GoodsAttribute $goodsAttribute */
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            return $this->sendError('Goods Attribute not found');
        }

        $goodsAttribute = $this->goodsAttributeRepository->update($input, $id);

        return $this->sendResponse($goodsAttribute->toArray(), 'GoodsAttribute updated successfully');
    }

    /**
     * Remove the specified GoodsAttribute from storage.
     * DELETE /goodsAttributes/{id}
     *
     * @param  int $id
     *
     * @return Response
     */
    public function destroy($id)
    {
        /** @var GoodsAttribute $goodsAttribute */
        $goodsAttribute = $this->goodsAttributeRepository->findWithoutFail($id);

        if (empty($goodsAttribute)) {
            return $this->sendError('Goods Attribute not found');
        }

        $goodsAttribute->delete();

        return $this->sendResponse($id, 'Goods Attribute deleted successfully');
    }
}
